package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.ForgetPwdModel;
import com.rjwh.dingdong.client.mvp.presenter.IForgetPwdPresenter;
import com.rjwh.dingdong.client.mvp.view.ForgetPwdView;
import com.rjwh.dingdong.client.util.ToastUtil;

/**
 * @author Kay
 * @ClassName ForgetPwdPresenter
 * @Description
 * @date 2016-7-29
 * @history 2016-7-29 author: description:
 */
public class ForgetPwdPresenter extends BasePresenter<ForgetPwdView> implements IForgetPwdPresenter {

    private ForgetPwdModel model;

    public ForgetPwdPresenter(Context context, ForgetPwdView mMvpView) {
        super(context, mMvpView);
        model = new ForgetPwdModel(this);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (isViewAttached()) {
            getMvpView().dismissLoading();
            switch (businessType) {
                case NetConstant.TYPE_SMS_CODE:
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS) {
                        getMvpView().setGetCodeBtnClick(false);
                        getMvpView().setTimerData();
                    } else {
                        if (mAsResult.getResultMsg() != null) {
                            ToastUtil.showToast(getContext(), mAsResult.getResultMsg());
                        }
                    }
                    break;
                case NetConstant.TYPE_CONFIRM_SMS_CODE:
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS) {
                        getMvpView().updataViewWithStep(2);
                    } else if (!AsStrUtils.isEmpty(mAsResult.getResultMsg())) {
                        ToastUtil.showToast(getContext(), mAsResult.getResultMsg());
                    }
                    break;
                case NetConstant.TYPE_MODIFY_WORD:
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS) {
                        getMvpView().finishActivity();
                    } else if (!AsStrUtils.isEmpty(mAsResult.getResultMsg())) {
                        ToastUtil.showToast(getContext(), mAsResult.getResultMsg());
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }

    /**
     * 获取验证码
     *
     * @param inputPhone 手机号
     */
    @Override
    public void doGetCode(String inputPhone) {
        if (isViewAttached()) {
            getMvpView().showLoading();
            model.doGetCode(inputPhone);
        }
    }

    /**
     * 校验验证码
     *
     * @param inputPhone 手机号
     * @param codeInput  验证码
     */
    @Override
    public void doCheckCode(String inputPhone, String codeInput) {
        if (isViewAttached()) {
            getMvpView().showLoading();
            model.doCheckCode(inputPhone, codeInput);
        }
    }

    /**
     * 确认修改密码
     *
     * @param currentPhone 手机号
     * @param passWord     密码
     * @param codeInput    验证码
     */
    @Override
    public void doModifyPwd(String currentPhone, String passWord, String codeInput) {
        if (isViewAttached()) {
            getMvpView().showLoading();
            model.doModifyPwd(currentPhone, passWord, codeInput);
        }
    }

}
